package com.mazaiting.redeye.module.user.login

import android.Manifest
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.mazaiting.easy.app.IApplicationComponent
import com.mazaiting.easy.base.activity.BaseActivity
import com.mazaiting.easy.base.activity.BaseLoadingActivity
import com.mazaiting.easy.base.interfaces.IPermissionStrategy
import com.mazaiting.easy.base.permission.BasePermissionStrategy
import com.mazaiting.face.ui.FaceDetectActivity
import com.mazaiting.face.ui.FaceManager
import com.mazaiting.log.L
import com.mazaiting.permission.PermissionResult
import com.mazaiting.permission.Permissions
import com.mazaiting.permission.util.PermissionUtil
import com.mazaiting.permission.util.State
import com.mazaiting.redeye.BuildConfig
import com.mazaiting.redeye.R
import com.mazaiting.redeye.base.ApplicationComponentImpl
import com.mazaiting.redeye.base.Config
import com.mazaiting.redeye.bean.UserBean
import com.mazaiting.redeye.module.face.DetectStrategyModule
import com.mazaiting.redeye.module.face.IDetectCallback
import com.mazaiting.redeye.module.main.MainActivity
import com.mazaiting.redeye.module.user.register.RegisterActivity
import com.mazaiting.redeye.utils.CosUtil
import com.umeng.analytics.MobclickAgent
import com.umeng.socialize.UMAuthListener
import com.umeng.socialize.UMShareAPI
import com.umeng.socialize.bean.SHARE_MEDIA
import kotlinx.android.synthetic.main.activity_login.*

/**
 * 登录页面
 */
@Permissions(
    value = [
        Manifest.permission.WRITE_EXTERNAL_STORAGE,
        Manifest.permission.READ_PHONE_STATE
    ], code = BaseActivity.REQUEST_PERMISSION_CODE
)
class LoginActivity : BaseLoadingActivity<LoginPresenter>(), LoginContract.View {
    override fun detectSuccess(msg: String) {
        Toast.makeText(this@LoginActivity, msg, Toast.LENGTH_SHORT).show()
    }

    override fun getContentLayout(): Int = R.layout.activity_login

    override fun isSlideLock(): Boolean = true

    override fun inject(applicationComponent: IApplicationComponent?) {
        DaggerLoginComponent
            .builder()
            .applicationComponentImpl(applicationComponent as ApplicationComponentImpl)
            .build()
            .inject(this)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data)
    }

    override fun bindView(view: View?, savedInstanceState: Bundle?) {
        super.bindView(view, savedInstanceState)
        // 登录点击
        login_acb_login.setOnClickListener { v -> onClick(v.id) }
        // 注册点击
        login_acb_register.setOnClickListener { v -> onClick(v.id) }
    }

    /**
     * 登录
     */
    private fun onClick(id: Int) {
        when (id) {
            R.id.login_acb_login -> {
//                startLoginActivity()
                onLogin()
            }
            R.id.login_acb_register -> {
                onRegister()
//                onQqLogin()
//                onFaceDetect()
//                onCos()
            }
        }
    }

    /**
     * 腾讯云对象存储
     */
    private fun onCos() {
        val cosUtil = CosUtil(this)
//        cosUtil.upload("", "")
        cosUtil.download("42b80f6a18f478d8639d3e96cc3024f8.png", Config.FILE_PATH)
    }

    private fun onFaceDetect() {
        val intent = Intent(this@LoginActivity, FaceDetectActivity::class.java)
        startActivity(intent)
    }

    private fun onQqLogin() {
        val authListener = object : UMAuthListener {
            override fun onComplete(platform: SHARE_MEDIA?, action: Int, data: MutableMap<String, String>?) {
                L.d("onComplete: $platform, action: $action, data: $data")
            }

            override fun onCancel(platform: SHARE_MEDIA?, action: Int) {
                L.d("onCancel: $platform, action: $action")
            }

            override fun onError(platform: SHARE_MEDIA?, action: Int, t: Throwable?) {
                L.d("onComplete: $platform, action: $action, t: ${t?.message}")
            }

            override fun onStart(platform: SHARE_MEDIA?) {
                L.d("onStart: $platform")
            }
        }
        UMShareAPI.get(this).getPlatformInfo(this@LoginActivity, SHARE_MEDIA.QQ, authListener)
    }

    /**
     * 注册
     */
    private fun onRegister() {
        val intent = Intent(this@LoginActivity, RegisterActivity::class.java)
        startActivity(intent)
    }

    /**
     * 登录
     */
    private fun onLogin() {
        // 用户名
        val username = login_tiet_username.text.toString()
        // 密码
        val password = login_tiet_password.text.toString()
        if (BuildConfig.DEBUG && TextUtils.isEmpty(username)) {
            login_tiet_username.setText("mazaiting5")
            login_tiet_password.setText("123456")
        }
        // 登录
        presenter?.login(UserBean(username, password))
    }

    override fun initData() {
        FaceManager.getInstance().detectStrategyModule = DetectStrategyModule(presenter)
//        sendSms()
//        val timer = Timer()
//        timer.schedule(object :TimerTask() {
//            override fun run() {
//                mPresenter?.hello()
//            }
//
//        }, 30 * 1000)
//        submitSms()
    }

    override fun loginSuccess() {
        MobclickAgent.onEvent(this, Config.UM_EVENT_LOGIN)
        L.d("loginSuccess")
        Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show()
        startLoginActivity()
    }

    /**
     * 开启登陆页面
     */
    private fun startLoginActivity() {
        val intent = Intent(this@LoginActivity, MainActivity::class.java)
        startActivity(intent)
    }

    override fun loginError(msg: String) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
    }

    override fun getPermissionStrategy(): IPermissionStrategy = object : BasePermissionStrategy() {
        override fun onDenied(permissions: MutableList<String>?) {
            PermissionUtil.requestPermission(this)
        }
    }
}
